require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')
require('../libs/iconfont/iconfont.css')
require('../css/register.less')
require('weui')

const axios = require('axios')

const CaptchaMini = require("captcha-mini")

const weui = require('weui.js')

require('../css/register.less')
document.addEventListener('DOMContentLoaded', function () {
    const phoneNum = document.querySelector('#phoneNum')
    const checkCode = document.querySelector('#checkCode')
    const checkPwd = document.querySelector('#checkPwd')
    const pwd = document.querySelector('#pwd')
    const inputCode = document.querySelector('#inputCode')
    const subBtn = document.querySelector('#subBtn')
    let letter = 'abcdefghijklmnopqrstuvwxyz'
    let check;//保存验证码值

    //验证码
    let captcha = new CaptchaMini({
        lineWidth: 1,   //线条宽度
        lineNum: 6,       //线条数量
        dotR: 2,          //点的半径
        dotNum: 25,       //点的数量
        preGroundColor: [10, 80],    //前景色区间
        backGroundColor: [150, 250], //背景色区间
        fontSize: 40,           //字体大小
        fontFamily: ['Georgia', '微软雅黑', 'Helvetica', 'Arial'],  //字体类型
        fontStyle: 'fill',      //字体绘制方法，有fill和stroke
        content: letter + letter.toLocaleUpperCase(),  //验证码内容
        length: 6    //验证码长度
    });
    //网页加载时初始化验证码画布
    captcha.draw(document.querySelector('#captcha'), function (r) {
        return check = r
    });
    // console.log(check);

    // 点击提交按钮依次验证合法性
    subBtn.addEventListener('click', function () {
        const phoneNumVal = phoneNum.value
        const inputCodeVal = inputCode.value
        const pwdVal = pwd.value
        const checkPwdVal = checkPwd.value
        if (!/^1[3-9][0-9]{9}$/.test(phoneNumVal)) {
            tips.textContent = '请输入手机号'
        } else if (inputCodeVal.toUpperCase() != check.toUpperCase()) {
            tips.textContent = '验证码不正确'

        } else if (!/^(?=.*?[a-z,A-Z])(?=.*?[0-9]).{6,}$/.test(pwdVal)) {
            tips.textContent = '至少6位密码（至少一个字母和一个数字）'
            tips.textContent.length > 8 ? tips.style.fontSize = '12px': ''
        } else if (pwdVal != checkPwdVal) {
            tips.textContent = '两次密码不一致'

        } else {
            tips.textContent = ''
            //提交数据
            axios
                .post("http://139.9.177.51:8099/users/add", {
                    account: phoneNumVal,
                    password: pwdVal,
                })
                .then(function (res) {
                    console.log(res.data);
                    if (res.data.status === 0) {
                        //成功-先弹一个轻提示
                        weui.toast('注册成功')
                        //再跳转页面-跳登录
                        location.href = "./login.html";
                    } else {
                        // console.log('注册失败');
                        weui.alert(res.data.msg)
                    }
                });
        }
    })
})